package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DepCliMonitorApplication;
import com.dhcc.DRGsHosp.core.domain.DepGroup;

@Named
@Transactional
public class DepCliMoniotrApplicationImpl implements DepCliMonitorApplication {
	
	private QueryChannelService queryChannel;
  
	@Autowired  
    private JdbcTemplateObj jdbcTemplateObj; 
	
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }

	@Override
	public List<DepGroup> getDepGroupCodeByDep(String depCode){
		
		List<DepGroup> depGroups = new ArrayList<DepGroup>();
		
//查询科室表
		StringBuilder jpql = new StringBuilder("select _department.parDepGroupCode from Department _department");
		jpql.append(" where _department.depCode='"+depCode+"'");

	   	List<String> arList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
	   	if(arList.size() > 0){
	   		for(String arData:arList){
	   			DepGroup depGroupCode = new DepGroup();
	   			depGroupCode.setDepGroupCode(arData);
	   			depGroups.add(depGroupCode);
	   		}
	   	}
	   	return depGroups;
	
	}


	@Override
	public List<DepGroup> getDepGroupName(String depGroupCode) {
		
		List<DepGroup> depGroups = new ArrayList<DepGroup>();
		
		//查询科室表
		StringBuilder jpql = new StringBuilder("select _depGroup.depGroupDesc from DepGroup _depGroup");
		jpql.append(" where _depGroup.depGroupCode='"+depGroupCode+"'");

	   	List<String> arList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
	   	if(arList.size() > 0){
	   		for(String arData:arList){
	   			DepGroup depGroupName = new DepGroup();
	   			depGroupName.setDepGroupDesc(arData);
	   			depGroups.add(depGroupName);
	   		}
	   	}
	   	return depGroups;
	
	}
	/**
	 * 获取科室组的汇总信息
	 * @param dto
	 * @return shaozhiyuan
	 */
	@Override
	public List<Map<String, Object>> getDepGroupSum(String depCodeStr) {
		//查询科室表
		StringBuilder sql = new StringBuilder();
		sql.append(" select dt.pardepgroup_code CODE ,dp.depgroup_desc DECC,dt.dep_code,dt.dep_name from drgs.t_department dt   \n");
		sql.append(" left join drgs.t_depgroup  dp on dt.pardepgroup_code=dp.depgroup_code \n");
		sql.append(" where dt.dep_code in ("+depCodeStr+")  \n");
		sql.append(" group by dt.pardepgroup_code,dp.depgroup_desc,dp.depgroup_order,dt.dep_code,dt.dep_name  order by dp.depgroup_order \n");
		List<Map<String,Object>> list =jdbcTemplateObj.queryForList(sql.toString()) ;
		return list;
	}

}